//package com.mianyang.zhangsan.common.util;
//
//import com.alibaba.fastjson.JSONObject;
//import org.apache.http.Consts;
//import org.apache.http.client.config.RequestConfig;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.client.methods.HttpRequestBase;
//import org.apache.http.config.Registry;
//import org.apache.http.config.RegistryBuilder;
//import org.apache.http.conn.socket.ConnectionSocketFactory;
//import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
//import org.apache.http.conn.socket.PlainConnectionSocketFactory;
//import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
//import org.apache.http.entity.ContentType;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
//import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.util.EntityUtils;
//import org.springframework.util.Assert;
//
//import javax.net.ssl.SSLContext;
//import java.io.IOException;
//import java.io.InputStream;
//import java.security.NoSuchAlgorithmException;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * http请求工具类
// * @author jiangjr
// * @date 2022-09-05
// */
//public class HttpHelper {
//    /**
//     * 连接池配置
//     */
//    private static final PoolingHttpClientConnectionManager CM;
//    /**
//     * 请求配置
//     */
//    private static final RequestConfig REQUEST_CONFIG;
//
//    static {
//        LayeredConnectionSocketFactory sslsf = null;
//        try {
//            sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
//        }catch (NoSuchAlgorithmException e){
//            System.out.println("error");
//        }
//        Assert.notNull(sslsf, "http连接池初始化异常");
//        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
//                .register("https", sslsf)
//                .register("http", new PlainConnectionSocketFactory())
//                .build();
//        CM = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
//        //最大连接数（多线程）
//        CM.setMaxTotal(200);
//        //每个路由并发数
//        CM.setDefaultMaxPerRoute(300);
//        REQUEST_CONFIG = RequestConfig.custom()
//                // 等待目标服务响应超时时间
//                .setSocketTimeout(20000)
//                // 连接目标服务器超时时间
//                .setConnectTimeout(20000)
//                // 从连接池获取连接超时时间
//                .setConnectionRequestTimeout(5000)
//                .build();
//    }
//
//    /**
//     * 获取http客户端
//     * @return http客户端
//     */
//    public static CloseableHttpClient getHttpClient(){
//        return HttpClients.custom()
//                .setConnectionManager(CM)
//                .build();
//    }
//
//    /**
//     * 关闭响应连接
//     * @param response response
//     * @throws IOException io
//     */
//    public static void closeResponse(CloseableHttpResponse response) throws IOException{
//        EntityUtils.consume(response.getEntity());
//        response.close();
//    }
//
//
//    /**
//     * get请求
//     * @param uri 请求基础路径
//     * @param headers 需要设置的请求头
//     * @param param 需要传递的参数
//     * @return 响应体
//     * @throws IOException io
//     */
//    public static InputStream get(String uri, Map<String, String> headers, JSONObject param) throws IOException{
//        CloseableHttpClient httpClient = getHttpClient();
//        CloseableHttpResponse closeableHttpResponse;
//        String url = uri;
//        // 封装请求参数
//        String parameter = formatParam(param);
//        if (StringUtils.isNotEmpty(parameter)){
//            url = uri + "?" + parameter;
//        }
//        // 创建get请求
//        HttpGet httpGet = new HttpGet(url);
//        // 设置请求头
//        setHeader(headers, httpGet);
//        // 设置请求配置
//        httpGet.setConfig(REQUEST_CONFIG);
//        closeableHttpResponse = httpClient.execute(httpGet);
//        InputStream content = closeableHttpResponse.getEntity().getContent();
//        closeResponse(closeableHttpResponse);
//        return content;
//    }
//
//    /**
//     * post请求
//     * @param uri 请求基础路径
//     * @param headers 请求头
//     * @param param 请求参数 json字符串
//     * @return 响应体
//     */
//    public static String post(String uri, Map<String, String> headers, JSONObject param) throws IOException{
//        CloseableHttpClient httpClient = getHttpClient();
//        CloseableHttpResponse closeableHttpResponse;
//        // 创建post请求
//        HttpPost httpPost = new HttpPost(uri);
//        // 设置请求头
//        setHeader(headers, httpPost);
//        // 设置请求参数
//        if (param != null){
//            StringEntity entity = new StringEntity(param.toString(), ContentType.APPLICATION_JSON.withCharset("utf-8"));
//            entity.setContentType("application/json");
//            entity.setContentEncoding("utf-8");
//            httpPost.setEntity(entity);
//        }
//        closeableHttpResponse = httpClient.execute(httpPost);
//        String response = EntityUtils.toString(closeableHttpResponse.getEntity());
//        closeResponse(closeableHttpResponse);
//        return response;
//    }
//
//    /**
//     * 设置请求头
//     * @param headers 请求头
//     * @param httpRequest request
//     */
//    private static void setHeader(Map<String, String> headers, HttpRequestBase httpRequest){
//        if (headers != null && !headers.isEmpty()){
//            for (String key : headers.keySet()){
//                httpRequest.setHeader(key, headers.get(key));
//            }
//        }
//        httpRequest.setHeader("Content-Type", "application/json");
//    }
//
//    /**
//     * 格式化请求参数
//     * @param param json字符串
//     * @return result
//     */
//    private static String formatParam(JSONObject param) throws IOException{
//        List<BasicNameValuePair> paramList = new ArrayList<>();
//        for (String key : param.keySet()) {
//            paramList.add(new BasicNameValuePair(key, param.get(key).toString()));
//        }
//        return EntityUtils.toString(new UrlEncodedFormEntity(paramList, Consts.UTF_8));
//    }
//
//}
//
